cap log close
log using "${logpath}specialization.log", replace

/*******************************************************************************
specialization.do

This code investigates specialization following litigation
*******************************************************************************/

qui {
	foreach YEAR of numlist 2007/2019{

		use if inlist(hcpcs_cd, "A0425", "A0426", "A0427", "A0428", "A0429", "A0430", "A0431", "A0432") | inlist(hcpcs_cd, "A0433", "A0434", "A0435", "A0436", "Q3019", "Q3020") using "${rawdatapath}car/`YEAR'/carl`YEAR'.dta", clear

		** Drop denied claims
		drop if linepmt == 0 

		* Match mileage to base rides
		gen base = 1 if hcpcs != "A0425" 
			* All others are base 
		replace base = 0 if mi(base)

		gen mileage =1 if hcpcs == "A0425"
		replace mileage = 0 if mi(mileage)

		bysort clm_id: egen hasbase = max(base)
		bysort clm_id: egen hasmileage = max(mileage)

		drop if hasbase == 0
		drop if hasmileage == 0 

		** Now only keep base rides 
		drop if mileage == 1 

		** Ambulance taxi flag 
		gen AT = 0 

		* Tag base rides that are to/from dialysis
			* drop whitespace in mdfr codes so they match 
		replace mdfr_cd1 = trim(mdfr_cd1)
		replace AT = 1 if (hcpcs_cd == "A0428") & (inlist(mdfr_cd1, "EG", "NG", "RG", "EJ", "NJ", "RJ") | inlist(mdfr_cd1, "GE", "GN", "GR", "JE", "JN", "JR"))

		*** Panelize by firm month AT 
		rename prf_npi npi 
		gen month = mofd(thru_dt)

		gen zip = substr(provzip, 1, 5)
		destring zip, replace force

		gen ride = 1 
		gen at_ride = 1 if AT == 1 
		replace at_ride = 0 if mi(at_ride)

		collapse (sum) ride (sum) at_ride (first) zip, by(npi month)

		tempfile claimtype`YEAR'
		save `claimtype`YEAR'', replace 


	}

	*** Create one big panel
	clear
	foreach YEAR of numlist 2007/2019{
		append using `claimtype`YEAR''
	} 

	**** Merge location-level enforcement

	merge m:1 zip using "${cleandatapath}zip_doj_crosswalk.dta", keep(master match) nogen 
	merge m:1 district using "${cleandatapath}DOJ_data_district.dta", nogen 
	rename (civ_backupdate crim_backupdate) (district_treat_civil district_treat_criminal)

	* Create criminal variable 
	gen postcrim = . 
	replace postcrim = 1 if month >= mofd(district_treat_criminal) & (month < mofd(district_treat_criminal) + 36)
	replace postcrim = 0 if month < mofd(district_treat_criminal) &!mi(district_treat_criminal) & month >= (mofd(district_treat_criminal) - 36)

	* Create civil variable 
	gen postciv = . 
	replace postciv = 1 if month >= mofd(district_treat_civil) & (month < mofd(district_treat_civil) + 36)
	replace postciv = 0 if month < mofd(district_treat_civil) &!mi(district_treat_civil) & month >= (mofd(district_treat_civil) - 36)

}

****************************
* By pre-post civ
preserve
qui drop if mi(postciv)
collapse (sum) ride (sum) at_ride, by(npi postciv)

qui drop if at_ride == 0
qui gen share_at = at_ride/ride

twoway (hist share_at if postciv == 0, width(.02499999) start(0))(hist share_at if postciv == 1, width(.02499999) start(0) lcolor(black) fcolor(none)), yti(Density) xti(Share of Rides for Non-Emergency Dialysis) graphregion(color(white)) bgcolor(white)  leg(order(1 "Pre-Civil Enforcement" 2 "Post-Civil Enforcement"))
qui graph export "${outpath}Figure_A13a.pdf", replace // Figure A13a

restore

* By pre-post crim 
preserve
qui drop if mi(postcrim)
collapse (sum) ride (sum) at_ride, by(npi postcrim)
qui drop if at_ride == 0
qui gen share_at = at_ride/ride 

twoway (hist share_at if postcrim == 0, width(.02499999) start(0)) (hist share_at if postcrim == 1, width(.02499999) start(0) lcolor(black) fcolor(none)), yti(Density) xti(Share of Rides for Non-Emergency Dialysis) graphregion(color(white)) bgcolor(white) leg(order(1 "Pre-Criminal Enforcement" 2 "Post-Criminal Enforcement"))
qui graph export "${outpath}Figure_A13b.pdf", replace // Figure A13b

restore

log close